package com.hwh.myapp

import android.content.Intent
import android.graphics.Rect
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.hwh.lib.base.adapter.CommonAdapter
import com.hwh.lib.base.adapter.CommonViewHolder
import com.hwh.lib.base.utils.DensityUtil
import com.hwh.lib.view.itemDecoration.LinearItemDecoration
import com.hwh.lib.view.itemDecoration.ProgressItemDecoration
import java.util.*
import kotlin.collections.ArrayList

class RecyclerviewTestActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_recyclerview_test)

        val list = ArrayList<Int>()
        for(i in 0..19) {
            list.add(i)
        }

        // https://blog.csdn.net/xingxtao/article/details/85219071 -> RecyclerView ItemDecoration 完全解析
        findViewById<RecyclerView>(R.id.recyclerView).run {
            layoutManager = LinearLayoutManager(context).apply { orientation = LinearLayoutManager.HORIZONTAL }
            adapter = CommonAdapter(list, object : CommonAdapter.OnBindDataListener<Int> {
                override fun onBindViewHolder(
                    model: Int,
                    viewHolder: CommonViewHolder,
                    type: Int,
                    position: Int
                ) {
                    viewHolder.itemView.setOnClickListener {
                        startActivity(Intent(this@RecyclerviewTestActivity, DialogActivity::class.java))
                    }
                }

                override fun getLayoutId(type: Int): Int = R.layout.item_test

            })
            addItemDecoration(object : RecyclerView.ItemDecoration() {
                override fun getItemOffsets(
                    outRect: Rect,
                    view: View,
                    parent: RecyclerView,
                    state: RecyclerView.State
                ) {
                    super.getItemOffsets(outRect, view, parent, state)
                    val pos = parent.getChildAdapterPosition(view)
                    Log.i("hwh", "pos = $pos , count = ${parent.adapter!!.itemCount} , count = ${parent.childCount} 不可用")
                    outRect.top = DensityUtil.dip2px(context, 10f)
                    outRect.bottom = DensityUtil.dip2px(context, 10f)
                    when (pos) {
                        0 -> {
                            outRect.left = DensityUtil.dip2px(context, 30f)
                            outRect.right = DensityUtil.dip2px(context, 10f)
                        }
                        parent.adapter!!.itemCount - 1 -> {
                            outRect.left = DensityUtil.dip2px(context, 10f)
                            outRect.right = DensityUtil.dip2px(context, 30f)
                        }
                        else -> {
                            outRect.left = DensityUtil.dip2px(context, 10f)
                            outRect.right = DensityUtil.dip2px(context, 10f)
                        }
                    }
                }
            })
        }

        findViewById<RecyclerView>(R.id.rv_recycler).run {

            val list: MutableList<String> = ArrayList()
            for (i in 0..9) {
                var data = "test data  - $i"
                if (i % 2 == 0) {
                    data = "$data\n这是测试数据 $i"
                }
                list.add(data)
            }

            layoutManager = LinearLayoutManager(context).apply { orientation = LinearLayoutManager.VERTICAL }
            adapter = CommonAdapter(list, object : CommonAdapter.OnBindDataListener<String> {
                override fun onBindViewHolder(
                    model: String,
                    viewHolder: CommonViewHolder,
                    type: Int,
                    position: Int
                ) {
                    viewHolder.setText(R.id.tv_title, model)
                    viewHolder.setText(R.id.tv_time, Date().toString())
                }

                override fun getLayoutId(type: Int): Int = R.layout.item_recycler_2

            })
//            addItemDecoration(LinearItemDecoration())
            addItemDecoration(ProgressItemDecoration(context).apply { setDoingPosition(this@run, 3) })
        }

    }
}